-- dep
-- https://github.com/cloudflare/lua-resty-cookie
local cookie_handle = require('resty.cookie')
local config = require('config.application')

-- perf
--local setmetatable = setmetatable

local Cookie = {}

function Cookie:new()
	local cookieobj=cookie_handle:new()
	local o={
		cookie=cookieobj
	}
	setmetatable(o,{__index=self})
	return o
end

function Cookie:set(name,value ,max_age ,path,domain, secure,httponly,extension)
	value= value or ''
	max_age= max_age or 0
	path=path or '/'
	domain=domain or config.domain
	secure=secure or false
	httponly=httponly or false

	if max_age~=0 then
		--ngx.update_time()
		expires=ngx.cookie_time(ngx.time()+max_age) 
	else
		expires=ngx.cookie_time(0) 
	end

	value=ngx.escape_uri(value)

	local t={
        key = name, value = value, path = path,
        domain = domain, secure = secure, httponly = httponly,
        expires = expires, max_age = max_age,
        extension = extension
	}
	return self.cookie:set(t)
end

function Cookie:get(key)
	local ok,error= self.cookie:get(key)
	if not ok then
		return ok,error
	end
	return ngx.unescape_uri(ok)
end

function Cookie:get_all()
	return self.cookie:get_all()
end


return Cookie